package com.aifast.core.auth;

import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.filter.SaServletFilter;
import cn.dev33.satoken.jwt.StpLogicJwtForStyle;
import cn.dev33.satoken.router.SaHttpMethod;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpLogic;
import com.aifast.common.global.Const;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * @Author fujian
 * @since 2022/1/14 15:52
 */
@Configuration
public class TokenConfig {

    @Bean
    @Primary
    public SaTokenConfig getMySaTokenConfig(){
        SaTokenConfig config = new SaTokenConfig();
        config.setTokenName(Const.TOKEN_HEADER);
        config.setTimeout(36000);
        config.setIsConcurrent(false);
        config.setIsReadCookie(false);
        config.setIsShare(false);
//        config.setTokenStyle("");
        config.setIsLog(true);
        config.setTokenPrefix("Bearer");
        config.setIsPrint(false);
        config.setJwtSecretKey("23ds8das434aisinjuaro0379fujian1993");
        return config;
    }

    /*跨域*/
    @Bean
    public SaServletFilter getSaServletFilter(){
        return new SaServletFilter().setBeforeAuth(i->{
            SaHolder.getResponse()
                    .setHeader("Access-Control-Allow-Origin", "*")
                    .setHeader("Access-Control-Allow-Methods", "POST, GET")
                    .setHeader("Access-Control-Max-Age", "3600")
                    .setHeader("Access-Control-Allow-Headers", "*")
                    .setHeader("X-XSS-Protection", "1; mode=block")
                    .setHeader("X-Content-Type-Options", "nosniff");

            SaRouter.match(SaHttpMethod.OPTIONS).free(r->{}).back();
        });
    }

    @Bean
    public StpLogic getStpLogicJwt() {
        return new StpLogicJwtForStyle();
    }

}
